import axios, { AxiosError, AxiosRequestConfig, AxiosResponse } from "axios";
import { useUserStore } from "../store/modules/User";
// 请求配置
const request = axios.create({
  baseURL: '',
  timeout: 30000,
  withCredentials: true
});

request.interceptors.request.use((config: AxiosRequestConfig) => {
  // 每次请求后把参数放在请求头里面
  const userStore = useUserStore();
  const token = userStore.getToken;
  if (token) {
    config.headers!.Authorization = token;
  }
  config.url = "api/responseData" + config.url + ".json";
  return config;
}, (error: AxiosError) => {
  return Promise.reject(error);
});

request.interceptors.response.use((response: AxiosResponse) => {
  return response;
}, (error: AxiosError) => {
  return Promise.reject(error);
});
export default request;